package com.centanet.framework.network;

import android.text.TextUtils;

import com.android.volley.Request;
import com.centanet.framework.interfaces.ILoggerTag;
import com.orhanobut.logger.Logger;

/**
 * json格式参数请求,请求方式：POST
 */
public abstract class JsonApi<T> extends AbstractFrameApi<T> {

    /**
     * @param tag               请求标签：Activity.class、Fragment.class、Service.class等等；
     * @param tClass            Gson解析目标实体；
     * @param iResponseListener 响应监听；
     */
    public JsonApi(Class<?> tag, Class<T> tClass, com.centanet.framework.interfaces.IResponseListener<T> iResponseListener) {
        super(tag, tClass, iResponseListener);
    }

    @Override
    protected int getMethod() {
        return Request.Method.POST;
    }

    @Override
    public StringBuilder url() {
        final String path = getPath();
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(getIp());
        if (TextUtils.isEmpty(path)) {
            Logger.t(ILoggerTag.FRAME_REQUEST).d("url : %s\nmethod: %s", urlBuilder.toString(), "POST");
        } else {
            urlBuilder.append(path);
            Logger.t(ILoggerTag.FRAME_REQUEST).d("url : %s\npath : %s\nmethod: %s", urlBuilder.toString(), path, "POST");
        }
        return urlBuilder;
    }

    @Override
    public byte[] params() {
        Class<?> aClass = getParams();
        if (aClass == null) {
            return new byte[0];
        } else {
            return new JsonEncode(aClass).encode(getParamsEncoding());
        }
    }

    /**
     * Json 格式参数
     */
    protected abstract Class<?> getParams();
}
